package cn.edu.neusoft.zengweidong707.neufood.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.activity.FoodDetailsActivity;
import cn.edu.neusoft.zengweidong707.neufood.bean.FoodListBean;

/**
 * Created by Lucio on 2017/11/30.
 */

public class FoodListAdapter extends BaseAdapter<FoodListBean> {

    public FoodListAdapter(Context context, List<FoodListBean> items) {
        super(context, items);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.items_foodlist, parent, false);
        FoodListVIewHolder foodListVIewHolder = new FoodListVIewHolder(itemView);
        return foodListVIewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final FoodListBean entity = (FoodListBean) items.get(position);
        if (null == entity)
            return;
        FoodListVIewHolder myViewHolder = (FoodListVIewHolder) holder;
        myViewHolder.tv_foodname.setText(entity.getFoodname());
        Double prices = entity.getPrice();
        myViewHolder.tv_price.setText(prices.toString());
        myViewHolder.tv_comment.setText(entity.getIntro());
        String photo = entity.getPic();
        if( photo == "" ){
            myViewHolder.img_card.setImageResource(R.mipmap.ic_launcher);
        }
        else{
            Picasso.with(context)
                    .load(photo)
                    .into(myViewHolder.img_card);
        }
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int foodid = entity.getFood_id();
                Intent intent = new Intent(context, FoodDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("foodid",foodid);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    public class FoodListVIewHolder extends RecyclerView.ViewHolder {
        ImageView img_card;
        TextView tv_foodname;
        TextView tv_comment;
        TextView tv_price;
        FoodListVIewHolder(View itemView) {
            super(itemView);
            img_card = (ImageView) itemView.findViewById(R.id.img_card);
            tv_foodname = (TextView) itemView.findViewById(R.id.tv_foodname);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        }
    }
}
